<?php
//------------------------------------------------------------------------------------------------------------------------------
//	Nom : lib_read.php
//	Auteurs: Defondemieres Fabrice - Delfour Kevin
//	Description :
//------------------------------------------------------------------------------------------------------------------------------
//Gestion des erreurs
//ini_set("error_reporting", E_ALL);
ini_set("display_errors","0"); 
ini_set("ignore_repeated_errors","0");
ini_set("log_errors", "0" ); 
ini_set("error_log", "errors/errors_lib_stat.txt" ); 

include ('Lib_Read.php');
include ('Lib_Stat_value.php');

$taille = 50;

function art2xml ($target){
  global $taille;
  $somme = 0;
  
    $data = "<chart>
	<axis_category size='14' color='000000' alpha='0' font='arial' bold='true' skip='0' orientation='horizontal' />
	<axis_ticks value_ticks='false' category_ticks='false' major_thickness='2' minor_thickness='1' minor_count='1' major_color='000000' minor_color='222222' position='outside' />
	<axis_value min='0' max='1' font='arial' bold='true' size='10' color='000000' alpha='50' steps='2' prefix='' suffix='' decimals='3' separator='' show_min='true' />
	<chart_border color='000000' top_thickness='2' bottom_thickness='2' left_thickness='2' right_thickness='2' />
	<chart_data>
		<row>
    <null/>";
	$array = file2array ($target);
	$string = array2html ($array);
	$string = clear($string);
	$array = preg_split("/[\s,]+/",$string);
	for ($i = $taille; $i < (sizeof($array)-$taille); $i++){
	     $data .= "<string>$i</string>\n";
	}
  	$data .= "</row>
  		<row>
  			<string>Values</string>
    ";
  $moy = getmoyenne("$target");
	for ($i = $taille; $i < (sizeof($array)-$taille); $i++){
    $res = calculAt ($array,$i,$taille);    
    $somme += $res;
     $data .= "<number>$res</number>\n";
  }
  
  $moy = getmoyenne("$target");
  $data .= "</row>
      <row>
  			<string>Moyenne</string>";
  for ($i = $taille; $i < (sizeof($array)-$taille); $i++){
       $data .= "<number>$moy</number>\n";
    }
    
  $data .= "
  </row>
	</chart_data>
	<chart_grid_h alpha='10' color='000000' thickness='1' />
	<chart_pref line_thickness='1' point_shape='none' fill_shape='false' />
	<chart_rect x='40' y='25' width='500' height='400' positive_color='3399FF' positive_alpha='50' negative_color='000000' negative_alpha='10' />
	<chart_type>Line</chart_type>
	<chart_value position='cursor' size='8' color='0000FF' alpha='75' background_color='aaffff' decimals='3' />
	<draw>
		<text color='0000FF' alpha='15' font='arial' rotation='-90' bold='true' size='50' x='-10' y='250' width='300' height='150' h_align='center' v_align='top'>Value</text>
		<text color='000000' alpha='15' font='arial' rotation='0' bold='true' size='60' x='0' y='0' width='320' height='300' h_align='left' v_align='bottom'>Output</text>
	</draw>
	<legend_rect x='100' y='350' width='10' height='10' margin='10' />
	<series_color>
		<color>FF0000</color>
		<color>00CC00</color>
	</series_color>
</chart>";
	return $data;
}
function calculAt ($array,$position,$taille){
  $denomA = 0;
  $denomB = 0;
	$array_before  = array_slice($array,$position-21,$taille); 
	$array_after   = array_slice($array,$position+1,$taille);
	$array_before  = array_count_values($array_before);
	$array_after   = array_count_values($array_after);
	$intersection = array_intersect_key ($array_before, $array_after);
	
  foreach ($intersection as $key => $value) {
    $intersection[$key] = $value * $array_after[$key];
  }
  $summ = array_sum($intersection);
  
  foreach ($array_before as $key => $value) {
    $denomB += $value * $value;
  }
  foreach ($array_after as $key => $value) {
    $denomA += $value * $value;
  }
  $denom = sqrt($denomB * $denomA);
  
  $result = $summ / $denom;
  return $result; 
}
function clear($string){
	static $symbols = 
		array(	'‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '‘', '’', '“', '”', 
			'•', '–', '—', '˜', '™', 'š', '›', 'œ', 'Ÿ', '€', 'Æ', 'Á', 'Â', 'À', 'Å', 
			'Ã', 'Ä', 'Ç', 'Ð', 'É', 'Ê', 'È', 'Ë', 'Í', 'Î', 'Ì', 'Ï', 'Ñ', 'Ó', 'Ô', 
			'Ò', 'Ø', 'Õ', 'Ö', 'Þ', 'Ú', 'Û', 'Ù', 'Ü', 'Ý', 'â', 'æ','å', 
			'ã', 'ä', 'ç', 'ð', 'ë', 'í', 'î', 'ì', 'ï', 'ñ', 'ó', 'ô', 
			'ò', 'ø', 'õ', 'ö', 'ß', 'þ', 'ú', 'û', 'ù', 'ü', 'ý', 'ÿ', '¡', '£', '¤', 
			'¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯', '°', '±', '²', '³', 
			'´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', '×', '÷', '¢', 
			'…', 'µ',"(@titre)","(titre)","(@soustitre)","(soustitre)","(@surligne)","(surligne)",
    "(@gras)","(gras)","(souligne)","(@souligne)","(ita)","(@ita)","/>");
	
	if (validateString($string, false)) {
		return str_replace($symbols,'',$string);
	} else {
		return $string;
	}
}
